package 网络编程.Text;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class TCPText7 {
    public static void main(String[] args) throws IOException {
        //改写TCPText6的服务端,让服务端能同时让多个客户端进行操作
        //思路是:用于循环和多线程.最优的写法是循环＋多线程
        ServerSocket ss = new ServerSocket(10006);
        Socket accept = null;
        BufferedOutputStream bo = null;
        BufferedWriter bw = null;
        /*
        只是用循环的弊端是:如果是文件过大,第一个客户端还没有运行完毕,那么多个客户端就会开在accept()这里
        while (true) {
            //监控客户端的连接
            accept = ss.accept();
            //生成随机id
            String replace = UUID.randomUUID().toString().replace("-", "");
            //关联保存客户端数据的路径

            bo = new BufferedOutputStream(new FileOutputStream("D:\\idea2022\\day.com.qiezi\\day_13\\src\\网络编程\\Text\\" + replace + ".jpg"));
            BufferedInputStream bi = new BufferedInputStream(accept.getInputStream());

            int a;
            byte[] bytes = new byte[1024];
            while ((a = bi.read(bytes)) != -1) {
                bo.write(bytes, 0, a);
            }
            //给与反馈
            bw = new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
            bw.write("上传成功");
            bw.newLine();

            //释放资源
            bw.close();
            bo.close();
            accept.close();
        }*/



        //使用线程池运行
        ThreadPoolExecutor pool=new ThreadPoolExecutor(
                3,
                16,
                60,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(2),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()
        );
        //使用循环＋多线程
        while (true){
            //监控客户端的连接
            accept = ss.accept();
            /*//使用多线程
            new Thread(new MyThread(accept)).start();*/
            //使用线程池
            pool.submit(new MyThread(accept));
        }
    }
}
class MyThread implements Runnable{
    Socket accept=null;
    public MyThread(Socket socket) {
        this.accept=socket;
    }

    @Override
    public void run() {

        BufferedWriter bw = null;
        try {
            //生成随机id
            String replace = UUID.randomUUID().toString().replace("-", "");
            //关联保存客户端数据的路径

             BufferedOutputStream bo = new BufferedOutputStream(new FileOutputStream("D:\\idea2022\\day.com.qiezi\\day_13\\src\\网络编程\\Text\\" + replace + ".jpg"));
            BufferedInputStream bi = new BufferedInputStream(accept.getInputStream());

            int a;
            byte[] bytes = new byte[1024];
            while ((a = bi.read(bytes)) != -1) {
                bo.write(bytes, 0, a);
            }
            //给与反馈
            bw = new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
            bw.write("上传成功");
            bw.newLine();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        finally {
            if (accept!=null){
            //释放资源
            try {
                accept.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }}


    }
}
